package com.hiultra.archive.service.impl;

import java.util.HashMap;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hiultra.archive.domain.vo.ArchiveFaceRecordReportVo;
import com.hiultra.archive.domain.vo.ResultEntity;
import com.hiultra.archive.utils.tool.ToolUtils;
import com.hiultra.common.constant.BusinessConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.hiultra.archive.mapper.ArchiveRoomFaceRecordsMapper;
import com.hiultra.archive.domain.ArchiveRoomFaceRecords;
import com.hiultra.archive.service.IArchiveRoomFaceRecordsService;

/**
 * 人员刷脸记录Service业务层处理
 *
 * @author 派大星
 * @date 2024-07-23
 */
@Service
public class ArchiveRoomFaceRecordsServiceImpl implements IArchiveRoomFaceRecordsService {
    @Autowired
    private ArchiveRoomFaceRecordsMapper archiveRoomFaceRecordsMapper;

    @Value("${pubKey}")
    private String pubKey;

    @Value("${priKey}")
    private String priKey;
    @Value("${urlIp}")
    private String urlIp;
    @Value("${archiveRoomFaceRecordsReportUrl}")
    private String archiveRoomFaceRecordsReportUrl;

    /**
     * 查询人员刷脸记录
     *
     * @param id 人员刷脸记录主键
     * @return 人员刷脸记录
     */
    @Override
    public ArchiveRoomFaceRecords selectArchiveRoomFaceRecordsById(Long id) {
        return archiveRoomFaceRecordsMapper.selectArchiveRoomFaceRecordsById(id);
    }

    /**
     * 查询人员刷脸记录列表
     *
     * @param archiveRoomFaceRecords 人员刷脸记录
     * @return 人员刷脸记录
     */
    @Override
    public List<ArchiveRoomFaceRecords> selectArchiveRoomFaceRecordsList(ArchiveRoomFaceRecords archiveRoomFaceRecords) {
        return archiveRoomFaceRecordsMapper.selectArchiveRoomFaceRecordsList(archiveRoomFaceRecords);
    }

    /**
     * 新增人员刷脸记录
     *
     * @param archiveRoomFaceRecords 人员刷脸记录
     * @return 结果
     */
    @Override
    public int insertArchiveRoomFaceRecords(ArchiveRoomFaceRecords archiveRoomFaceRecords) {
        return archiveRoomFaceRecordsMapper.insertArchiveRoomFaceRecords(archiveRoomFaceRecords);
    }

    /**
     * 修改人员刷脸记录
     *
     * @param archiveRoomFaceRecords 人员刷脸记录
     * @return 结果
     */
    @Override
    public int updateArchiveRoomFaceRecords(ArchiveRoomFaceRecords archiveRoomFaceRecords) {
        return archiveRoomFaceRecordsMapper.updateArchiveRoomFaceRecords(archiveRoomFaceRecords);
    }

    /**
     * 批量删除人员刷脸记录
     *
     * @param ids 需要删除的人员刷脸记录主键
     * @return 结果
     */
    @Override
    public int deleteArchiveRoomFaceRecordsByIds(Long[] ids) {
        return archiveRoomFaceRecordsMapper.deleteArchiveRoomFaceRecordsByIds(ids);
    }

    /**
     * 删除人员刷脸记录信息
     *
     * @param id 人员刷脸记录主键
     * @return 结果
     */
    @Override
    public int deleteArchiveRoomFaceRecordsById(Long id) {
        return archiveRoomFaceRecordsMapper.deleteArchiveRoomFaceRecordsById(id);
    }

    /**
     * 档案室刷脸记录上报接口
     *
     * @param archiveRoomFaceRecords 人员刷脸记录
     * @return 结果
     */
    @Override
    public int reportArchiveRoomFaceRecords(ArchiveRoomFaceRecords archiveRoomFaceRecords) {

        ArchiveFaceRecordReportVo archiveRoomFaceRecordsVo = ToolUtils.convertEntity(archiveRoomFaceRecords, ArchiveFaceRecordReportVo::new);
        if (ObjectUtil.isNotNull(archiveRoomFaceRecords.getTime())) {
            archiveRoomFaceRecordsVo.setTime(DateUtil.format(archiveRoomFaceRecords.getTime(), "yyyyMMddHHmmss"));
        }
        String jsonString = "";
        try {
            jsonString = new ObjectMapper().writeValueAsString(archiveRoomFaceRecordsVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(jsonString);

        //发送请求获取数据
        HashMap hashMap = ToolUtils.sendPostRequest(BusinessConstants.ARCHIVE_ROOM_FACE_RECORDS_REPORT_NO, urlIp, archiveRoomFaceRecordsReportUrl, "人员刷脸记录上传", jsonString, pubKey, priKey);
        System.out.println("人员刷脸记录上传URL" + archiveRoomFaceRecordsReportUrl);
        System.out.println("人员刷脸记录上传信息" + hashMap.toString());
        if (hashMap.get("code").equals("00000")) {
            archiveRoomFaceRecords.setPushStatus(BusinessConstants.END_PUSHSTATUS);
            archiveRoomFaceRecordsMapper.updateArchiveRoomFaceRecords(archiveRoomFaceRecords);
            System.out.println("人员刷脸记录上传信息成功,推送状态修改为已推送！");
        } else {
            System.out.println("人员刷脸记录上传信息失败");
        }
        return 0;
    }

    /**
     * 档案室刷脸记录定时上报接口
     *
     * @return 结果
     */
    @Override
    public ResultEntity timingArchiveRoomFaceRecordsResultReport() {
        List<ArchiveRoomFaceRecords> archiveRoomFaceRecordsList = archiveRoomFaceRecordsMapper.selectArchiveRoomFaceRecordsList(new ArchiveRoomFaceRecords() {{
            setPushStatus(BusinessConstants.PUSHSTATUS);
        }});
        if (archiveRoomFaceRecordsList.size() > 0) {
            for (ArchiveRoomFaceRecords archiveRoomFaceRecords : archiveRoomFaceRecordsList) {
                reportArchiveRoomFaceRecords(archiveRoomFaceRecords);
            }
        }
        return ResultEntity.success();
    }
}
